import {
	get
} from '@/utils/axios.js'
import {
	queryTabList
} from '@/service/govern'

// 获取字典数据
export function getDictData(dictType) {
	return new Promise((resolve, reject) => {
		get('/api/v1/dictionary/findByDictionaryCode', {
			dictionaryCode: dictType
		}).then(res => {
			resolve(res.body.list || [])
		}).catch(() => reject())
	})
}

// 获取tab列表
export function getTabList(type) {
	return new Promise((resolve, reject) => {
		queryTabList({
			type: type
		}).then((res) => {
			resolve(res.data || [])
		}).catch(() => reject())
	})
}

// 文件大小转换
export function conver(limit) {
	var size = ''
	if (limit) {
		if (limit < 0.1 * 1024) {
			//如果小于0.1KB转化成B
			size = limit.toFixed(2) + 'B'
		} else if (limit < 0.1 * 1024 * 1024) {
			//如果小于0.1MB转化成KB
			size = (limit / 1024).toFixed(2) + 'KB'
		} else if (limit < 0.1 * 1024 * 1024 * 1024) {
			//如果小于0.1GB转化成MB
			size = (limit / (1024 * 1024)).toFixed(2) + 'MB'
		} else {
			//其他转化成GB
			size = (limit / (1024 * 1024 * 1024)).toFixed(2) + 'GB'
		}
	}
	var sizestr = size + ''
	var len = sizestr.indexOf('.')
	var dec = sizestr.substr(len + 1, 2)
	if (dec == '00') {
		//当小数点后为00时 去掉小数部分
		return sizestr.substring(0, len) + sizestr.substr(len + 3, 2)
	}
	return sizestr
}

// 数组赋值
export function listAssign(arrA, arrB) {
	if (arrB) {
		Object.keys(arrA).forEach((key) => {
			arrA[key] = arrB[key] || arrA[key]
		})
	}
}